﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Eassets.Service;
using Eassets.Service.Asset;
using Eassets.Layered.Domain.Asset;

namespace Eassets.UI.Asset
{
    public partial class FixedAssetsForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                GetAllMainCategory();
                GetAllAssets();

                ViewState["ButtonBehaviour"] = "Load";
                DisableComponants();
                //    TextBoxAssetCodeAdd.Attributes.Add("onChange", "javascript:this.value=toUpperCase(this.value);");
                //    TextBoxAssetCodeEdit.Attributes.Add("onChange", "javascript:this.value=toUpperCase(this.value);");
            }
        }

        private void GetAllMainCategory()
        {
            dropDownListMainCategory.Items.Clear();
            CategoryService categoryService = new CategoryService();
            Category category = new Category();
            try
            {
                dropDownListMainCategory.DataSource = categoryService.GetAllAssetCategory().Where(x => x.ParentId == 0).ToList();

                dropDownListMainCategory.DataValueField = "CategoryId";
                dropDownListMainCategory.DataTextField = "Description";
                dropDownListMainCategory.DataBind();
            }
            catch (Exception exception)
            {
                throw;
            }
        }


        private void GetAllSubCategory()
        {
            dropDownListSubCategory.Items.Clear();
            CategoryService categoryService = new CategoryService();
            Category category = new Category();
            try
            {
                dropDownListSubCategory.DataSource = categoryService.GetAllAssetCategory().Where(x => x.ParentId == Convert.ToInt32(dropDownListMainCategory.SelectedValue)).ToList();

                dropDownListSubCategory.DataValueField = "CategoryId";
                dropDownListSubCategory.DataTextField = "Description";
                dropDownListSubCategory.DataBind();
            }
            catch (Exception exception)
            {
                throw;
            }
        }

        protected void dropDownListMainCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetAllSubCategory();
        }

        protected void dropDownListSubCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetAllAssetBySubCategoryId();
        }

        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            ViewState["ButtonBehaviour"] = "Add";
            ViewState["ButtonBehaviour"] = "Add";
            DisableComponants();
        }

        protected void ButtonModify_Click(object sender, EventArgs e)
        {
            ViewState["checkAddorModify"] = "Modify";
            ViewState["ButtonBehaviour"] = "Modify";
            DisableComponants();
        }

        protected void ButtonDelete_Click(object sender, EventArgs e)
        {
            DisableComponants();
        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            DisableComponants();
            Assets asset = new Assets();
            AssetService assetService = new AssetService();

            if (Page.IsValid) // Validate the controls
            {

                if (ViewState["ButtonBehaviour"].ToString().Trim() == "Add")
                {
                    asset.AssetCategoryId = Convert.ToInt32(dropDownListSubCategory.SelectedValue);
                    asset.Code = TextBoxAssetCode.Text;
                    asset.Description = textBoxDiscription.Text;
                    asset.MoreDetails = textBoxMoreDetails.Text;
                    asset.SysDateCreation = DateTime.Today;
                    asset.SysDateLastModification = DateTime.Today;
                    try
                    {
                        assetService.AddAsset(asset);
                        FlashMessage1.Message = "Saved Successfully";
                        FlashMessage1.CssClass = "okmessage";
                        FlashMessage1.Display();

                    }
                    catch (Exception ex)
                    {
                        FlashMessage1.Message = "Save Failed";
                        FlashMessage1.CssClass = "errormessage";
                        FlashMessage1.Display();
                      
                    }

                    Response.Redirect("FixedAssetsForm.aspx");
                }
                else if (ViewState["ButtonBehaviour"].ToString().Trim() == "Modify")
                {
                    asset.AssetId = Convert.ToInt64(ViewState["AssetId"]);
                    asset.AssetCategoryId = Convert.ToInt32(dropDownListSubCategory.SelectedValue);
                    asset.Code = TextBoxAssetCode.Text;
                    asset.Description = textBoxDiscription.Text;
                    asset.MoreDetails = textBoxMoreDetails.Text;

                    try
                    {
                        assetService.ModifyAsset(asset);
                        FlashMessage1.Message = "Saved Successfully";
                        FlashMessage1.CssClass = "okmessage";
                        FlashMessage1.Display();
                    }
                    catch (Exception ex)
                    {
                        FlashMessage1.Message = "Save Failed";
                        FlashMessage1.CssClass = "errormessage";
                        FlashMessage1.Display();
                        Response.Redirect("~/Error.aspx");
                    }

                    Response.Redirect("FixedAssetsForm.aspx");
                }

            }
        }

        protected void ButtonClear_Click(object sender, EventArgs e)
        {

        }

        private void GetAllAssets()
        {
            AssetService assetService = new AssetService();
            Assets asset = new Assets();

            try
            {

                gridViewFixedAssets.DataSource = assetService.GetAllAsset();
                gridViewFixedAssets.DataBind();

            }
            catch (Exception exception)
            {


            }
        }

        private void GetAllAssetBySubCategoryId()
        {
            AssetService assetService = new AssetService();
            gridViewFixedAssets.DataSource = assetService.GetAllAsset().Where(x => x.AssetCategoryId == Convert.ToInt32(dropDownListSubCategory.SelectedValue)).ToList();
            gridViewFixedAssets.DataBind();
        }

        protected void gridViewFixedAssets_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["ButtonBehaviour"] = "GridSelectedChanged";
            DisableComponants();
            GridViewRow row = gridViewFixedAssets.SelectedRow;
            ViewState["AssetId"]=row.Cells[1].Text.Trim();
            TextBoxAssetCode.Text = row.Cells[2].Text.Trim();
            textBoxDiscription.Text = row.Cells[3].Text.Trim();
            textBoxMoreDetails.Text = row.Cells[4].Text.Trim();

        }

        protected void DisableComponants()
        {
            if (ViewState["ButtonBehaviour"] == ("Add"))
            {
                buttonModify.Enabled = false;
                buttonDelete.Enabled = false;
                TextBoxAssetCode.Enabled = true;
                TextBoxAssetCode.Text = String.Empty;
                textBoxDiscription.Enabled = true;
                textBoxDiscription.Text = string.Empty;
                textBoxMoreDetails.Enabled = true;
                textBoxMoreDetails.Text = String.Empty;

                TextBoxAssetCode.Enabled = true;
                TextBoxAssetCode.Text = string.Empty;
                textBoxDiscription.Enabled = true;
                textBoxDiscription.Text = string.Empty;
                textBoxMoreDetails.Enabled = true;
                textBoxMoreDetails.Text = string.Empty;

            }
            else if (ViewState["ButtonBehaviour"] == "Modify")
            {
                buttonAdd.Enabled = false;
                buttonDelete.Enabled = false;
                TextBoxAssetCode.Enabled = true;
                textBoxDiscription.Enabled = true;
                textBoxMoreDetails.Enabled = true;


            }
            else if (ViewState["ButtonBehaviour"] == "Delete")
            {

            }
            else if (ViewState["ButtonBehaviour"] == "Save")
            {

            }
            else if (ViewState["ButtonBehaviour"] == "Clear")
            {

            }

            else if (ViewState["ButtonBehaviour"] == "Load")
            {
                TextBoxAssetCode.Enabled = false;
                TextBoxAssetCode.Text = string.Empty;
                textBoxDiscription.Enabled = false;
                textBoxDiscription.Text = string.Empty;
                buttonAdd.Enabled = true;
                textBoxMoreDetails.Enabled = false;

                buttonModify.Enabled = false;
                buttonDelete.Enabled = false;
                buttonDelete.Enabled = false;

            }
            else if (ViewState["ButtonBehaviour"] == "GridSelectedChanged")
            {
                buttonAdd.Enabled = false;
                buttonModify.Enabled = true;
                buttonDelete.Enabled = true;
                TextBoxAssetCode.Enabled = false;
                textBoxDiscription.Enabled = false;
                textBoxMoreDetails.Enabled = false;
            }

        }


    }
}